package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.platform.PlatformForbidden;
import com.yanqu.road.entity.servercenter.whitelist.WhiteAccount;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlatformUserIdDaoImpl extends TempDao {

	public void getPlatformUserIdList(List<String> platformUserIdList) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_platform_userId";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				String content = rs.getString("platformUserId");
				if(null != content) {
					platformUserIdList.add(content);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
	}

	public boolean updateLoginTime(String platformUserId,long loginTime) {
		PreparedStatement call = null;
		String sql = "update t_s_platform_userId set lastLoginTime=? where platformUserId=? ";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++,loginTime);
			call.setString(i++, platformUserId);
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}


	public Map<String, Map<Integer, PlatformForbidden>> getForbiddenPlatformUserIdList() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_forbidden_platform_userId";
		Map<String, Map<Integer, PlatformForbidden>> platformUserIdMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				String platformUserId = rs.getString("platformUserId");
				int type = rs.getInt("forbiddenType");
				long startTime = rs.getLong("forbiddenStartTime");
				long ft = rs.getLong("forbiddenTime");
				int time = rs.getInt("times");

				PlatformForbidden platformForbidden = new PlatformForbidden(platformUserId, type, startTime, ft, time);

				if (!platformUserIdMap.containsKey(platformUserId)) {
					platformUserIdMap.put(platformUserId, new ConcurrentHashMap<>());
				}
				platformUserIdMap.get(platformUserId).put(type, platformForbidden);

			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return platformUserIdMap;
	}

	public boolean addForbiddenInfo(PlatformForbidden platformForbidden) {
		PreparedStatement call = null;
		String sql = "insert into t_s_forbidden_platform_userId (platformUserId,forbiddenType,forbiddenStartTime,forbiddenTime,times) values (?,?," +
				"?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++,platformForbidden.getPlatFormUserId());
			call.setInt(i++,platformForbidden.getForbiddenType());
			call.setLong(i++,platformForbidden.getForbiddenStartTime());
			call.setLong(i++,platformForbidden.getForbiddenTime());
			call.setInt(i++,platformForbidden.getTimes());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
            logger.error("{}:", Thread.currentThread().getStackTrace()[1].getMethodName(), e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean updateForbiddenInfo(PlatformForbidden platformForbidden) {
		PreparedStatement call = null;
		String sql = "update t_s_forbidden_platform_userId set forbiddenStartTime=?,forbiddenTime=?,times=? where platformUserId=? and " +
				"forbiddenType=?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++,platformForbidden.getForbiddenStartTime());
			call.setLong(i++,platformForbidden.getForbiddenTime());
			call.setInt(i++,platformForbidden.getTimes());
			call.setString(i++,platformForbidden.getPlatFormUserId());
			call.setInt(i++,platformForbidden.getForbiddenType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error("{}:", Thread.currentThread().getStackTrace()[1].getMethodName(), e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public Map<Long, Map<Integer, Long>> getCmdForbiddenMap(int serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_cmd_forbidden where serverId =" + serverId;

		long currentTimeMillis = System.currentTimeMillis();

		Map<Long, Map<Integer, Long>> cmdForbiddenMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				long userId = rs.getLong("userId");
				int cmdCode = rs.getInt("cmdCode");
				long endTime = rs.getLong("endTime");

				// 超时则不加载
				if (currentTimeMillis > endTime) {
					continue;
				}

				if (!cmdForbiddenMap.containsKey(userId)) {
					cmdForbiddenMap.put(userId, new ConcurrentHashMap<>());
				}
				cmdForbiddenMap.get(userId).put(cmdCode, endTime);

			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return cmdForbiddenMap;
	}

	public List<Long> getTokenWhiteUserIdList() {
		ResultSet rs = null;
		CallableStatement call = null;
		List<Long> dataList = new ArrayList<>();
		String sql = "select * from t_s_token_white_list";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				dataList.add(rs.getLong("userId"));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public List<WhiteAccount> getWhiteAccounts() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_platform_userId";
		List<WhiteAccount> res = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String platformUserId = rs.getString("platformUserId");
				Date lastLoginTime = rs.getDate("lastLoginTime");
				String remark = rs.getString("remark");
				res.add(new WhiteAccount(id, platformUserId, lastLoginTime, remark));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return res;
	}
}
